<template>
  <div class="admin-page">
    <el-container class="main-container">
      <!-- 侧边栏 - 使用弹性布局 -->
      <el-aside class="sidebar" :style="{ width: sidebarWidth }">
        <div class="sidebar-header">
          <img src="@/assets/logo.svg" alt="Logo" class="logo">
          <div v-if="!isSidebarCollapsed" class="sidebar-title">管理员端</div>
        </div>

        <el-menu
            class="sidebar-menu"
            active-text-color="#1890ff"
            background-color="white"
            default-active='/admin-home'
            text-color="black"
            @open="handleOpen"
            @close="handleClose"
            @select="handleSelect"
            :router="true"
            :collapse="isSidebarCollapsed"

        >
          <el-menu-item index="/admin-home">
            <el-icon><HomeFilled /></el-icon>
            <span slot="title">首页</span>
          </el-menu-item>

          <el-sub-menu index="manage">
            <template #title>
              <el-icon><EditPen /></el-icon>
              <span>信息管理</span>
            </template>
            <el-menu-item index="/manage-student">
              <el-icon><User /></el-icon>
              <span>学生管理</span>
            </el-menu-item>
            <el-menu-item index="/manage-teacher">
              <el-icon><Avatar /></el-icon>
              <span>教师管理</span>
            </el-menu-item>
            <el-menu-item index="/manage-major">
              <el-icon><Money /></el-icon>
              <span>专业管理</span>
            </el-menu-item>
            <el-menu-item index="/manage-college">
              <el-icon><School /></el-icon>
              <span>学院管理</span>
            </el-menu-item>
            <el-menu-item index="/manage-course">
              <el-icon><Tickets /></el-icon>
              <span>课程管理</span>
            </el-menu-item>
            <el-menu-item index="/manage-class">
              <el-icon><List /></el-icon>
              <span>班级管理</span>
            </el-menu-item>
            <el-menu-item index="/manage-lecture">
              <el-icon><Document /></el-icon>
              <span>授课管理</span>
            </el-menu-item>
          </el-sub-menu>

          <el-menu-item index="/score-review">
            <el-icon><Tickets /></el-icon>
            <span>成绩审核</span>
          </el-menu-item>
        </el-menu>

        <div class="sidebar-footer">
          <el-button
              size="default"
              @click="exit"
          >
            <span>退出登录</span>
          </el-button>
        </div>
      </el-aside>

      <!-- 主内容区 - 使用弹性布局 -->
      <el-container class="content-container">
        <el-header class="header">
          <div class="header-content">
            <el-button
                size="small"
                @click="toggleSidebar"
            >
              <el-icon><IconMenu /></el-icon>
            </el-button>
            <div class="header-title">{{ title }}</div>
          </div>
        </el-header>

        <el-main class="main-content">
          <router-view></router-view>
        </el-main>
      </el-container>
    </el-container>
  </div>
</template>

<script setup>
import { Location, Menu as IconMenu, Document, Setting, HomeFilled, EditPen, User, Avatar, Money, School, Tickets, List } from '@element-plus/icons-vue';
import {onMounted, ref, computed, watch, onBeforeMount} from "vue";
import { useRouter } from "vue-router";
import request from "@/requests/request.js";

const router = useRouter();
const isSidebarCollapsed = ref(false);
const title = ref('管理员/首页');

// 计算属性 - 侧边栏宽度
const sidebarWidth = computed(() => {
  return isSidebarCollapsed.value ? '64px' : '200px';
});

// 计算属性 - Logo宽度
const logoWidth = computed(() => {
  return isSidebarCollapsed.value ? '30px' : '100px';
});

// 检查登录状态
onMounted(() => {
  if (localStorage.getItem('token') === null) {
    router.push('/login')
  } else {
    request.get('/user/auth-admin').then(res => {
      if (res.data.code !== 200) {
        router.push('/login')
      } else {
        // 初始化标题
        const currentPath = router.currentRoute.value.path;
        const matchedTitle = titleIndex.find(item => item.index === currentPath);
        if (matchedTitle) {
          title.value = matchedTitle.title;
        }
      }
    }).catch(err => {
      router.push('/login')
    })
  }

  // 监听路由变化，更新标题
  router.beforeEach((to, from, next) => {
    const matchedTitle = titleIndex.find(item => item.index === to.path);
    if (matchedTitle) {
      title.value = matchedTitle.title;
    }
    next();
  });

  // 监听窗口大小变化，在小屏幕上默认折叠侧边栏
  const handleResize = () => {
    if (window.innerWidth < 768) {
      isSidebarCollapsed.value = true;
    } else {
      isSidebarCollapsed.value = false;
    }
  };

  window.addEventListener('resize', handleResize);
  handleResize(); // 初始化调用一次

  // 清理事件监听器
  onBeforeMount(() => {
    window.removeEventListener('resize', handleResize);
  });
});

const titleIndex = [
  { index: '/admin-home', title: '管理员/首页' },
  { index: '/manage-student', title: '管理员/信息管理/学生管理' },
  { index: '/manage-teacher', title: '管理员/信息管理/教师管理' },
  { index: '/manage-major', title: '管理员/信息管理/专业管理' },
  { index: '/manage-college', title: '管理员/信息管理/学院管理' },
  { index: '/manage-course', title: '管理员/信息管理/课程管理' },
  { index: '/manage-class', title: '管理员/信息管理/班级管理' },
  { index: '/score-review', title: '管理员/成绩审核' },
  { index: '/manage-lecture', title: '管理员/信息管理/授课管理' }
];

const handleOpen = (index) => {
  console.log(index);
};

const handleClose = (key, keyPath) => {
  console.log(key, keyPath);
};

const handleSelect = (key) => {
  const matchedItem = titleIndex.find(item => item.index === key);
  if (matchedItem) {
    title.value = matchedItem.title;
  }
};

const toggleSidebar = () => {
  isSidebarCollapsed.value = !isSidebarCollapsed.value;
};

const exit = () => {
  request.post('/to-logout').then(res => {
    if (res.data.code === 200) {
      localStorage.removeItem('token');
      router.push('/login');
    }
  }).catch(err => {
    console.log(err);
  });
};
</script>

<style scoped>
/* 主容器 - 使用弹性布局 */
.admin-page {
  height: 100vh;
  width: 100vw;
}
.main-container {
  display: flex;
  flex-direction: row;
  height: 100vh;
  width: 100vw;
  overflow: hidden;
}

/* 侧边栏 - 使用弹性布局 */
.sidebar {
  display: flex;
  flex-direction: column;
  background-color: white;
  border-right: 1px solid #e6e6e6;
  transition: width 0.3s ease;
  height: 100vh;
}

.sidebar-header {
  display: flex;
  flex-direction: column;
  align-items: center;
  padding: 16px;
  border-bottom: 1px solid #e6e6e6;
}

.sidebar-title {
  margin-top: 10px;
  font-weight: bold;
  font-size: 18px;
}

.sidebar-menu {
  flex-grow: 1;
  border-right: none !important;
}

.sidebar-footer {
  padding: 10px;
  border-top: 1px solid #e6e6e6;
  text-align: center;
}

/* 内容容器 - 使用弹性布局 */
.content-container {
  display: flex;
  flex-direction: column;
  flex-grow: 1;
  overflow: hidden;
}

.header {
  background-color: white;
  box-shadow: 0 2px 8px #f0f1f2;
  z-index: 1;
  height: 50px;
  padding: 0 16px;
  display: flex;
  align-items: center;
}

.header-content {
  display: flex;
  align-items: center;
  width: 100%;
}

.header-title {
  margin-left: 16px;
  font-size: 16px;
  font-weight: 500;
}

.main-content {
  flex-grow: 1;
  padding: 20px;
  background-color: #f0f2f4;
  overflow: auto;
  width: 100vw;
}

/* 移动端优化 */
@media (max-width: 768px) {
  .sidebar {
    position: fixed;
    left: 0;
    top: 0;
    z-index: 1000;
    transform: translateX(-100%);
    transition: transform 0.3s ease;
  }

  .sidebar.open {
    transform: translateX(0);
  }

  .header-title {
    margin-left: 8px;
  }
}

.logo {
  width: 48px;
  height: 48px;
  margin-bottom: 10px;
}
</style>
